﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Data.SqlClient;

namespace AlgoLogicMessenger
{
    /// <summary>
    /// Interaction logic for NavigationPage.xaml
    /// </summary>
    public partial class NavigationPage : Page
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);

        string connstr = @"Data Source=.;Initial Catalog=AlgoLogicDB;Integrated Security=True";

        public NavigationPage()
        {
            InitializeComponent();

            clientSocket.Connect(IP.Address, 8888);
            serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("" + "#");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            NavigationFrame.Content = null;

            //btnNavigation.Content = "Allow competitors" + Environment.NewLine + "to navigate";
        }

        //private void Quiz_Click(object sender, RoutedEventArgs e)
        //{
        //    byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@quizStart|#");
        //    if (serverStream != null)
        //    {
        //        serverStream.Write(outStream, 0, outStream.Length);
        //        serverStream.Flush();
        //    }

        //    QuestionAnswer QA = new QuestionAnswer();
        //    NavigationFrame.Content = QA;
        //}

        //private void Video_Click(object sender, RoutedEventArgs e)
        //{
        //    byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@videoStart|#");
        //    if (serverStream != null)
        //    {
        //        serverStream.Write(outStream, 0, outStream.Length);
        //        serverStream.Flush();
        //    }

        //    Videos V = new Videos();
        //    NavigationFrame.Content = V;
        //}

        //private void ID_Click(object sender, RoutedEventArgs e)
        //{
        //   ImportData im = new ImportData();
        //   NavigationFrame.Content = im;
        //}

        //private void GR_Click(object sender, RoutedEventArgs e)
        //{
        //    //GENERATE REPORT
        //    Report r = new Report();
        //    NavigationFrame.Content = r;
        //}

        //private void btnNavigation_Click(object sender, RoutedEventArgs e)
        //{
        //    try
        //    {
        //        if (btnNavigation.Content.ToString() == "Allow competitors\r\nto navigate")
        //        {
        //            string msg = "@AllowNavigation|";
        //            byte[] outStream1 = System.Text.Encoding.ASCII.GetBytes(msg + "#");
        //            if (serverStream != null)
        //            {
        //                serverStream.Write(outStream1, 0, outStream1.Length);
        //                serverStream.Flush();
        //            }
        //            MessageBox.Show("Competitors navigation service is allowed !");
        //            btnNavigation.Content = "Does not allow\r\ncompetitors to navigate";
        //        }
        //        else if (btnNavigation.Content.ToString() == "Does not allow" + Environment.NewLine + "competitors to navigate")
        //        {
        //            string msg = "@DisallowNavigation|";
        //            byte[] outStream1 = System.Text.Encoding.ASCII.GetBytes(msg + "#");
        //            if (serverStream != null)
        //            {
        //                serverStream.Write(outStream1, 0, outStream1.Length);
        //                serverStream.Flush();
        //            }
        //            MessageBox.Show("Competitors navigation service is disallowed !");
        //            btnNavigation.Content = "Allow competitors" + Environment.NewLine + "to navigate";
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }
        //}

        
        //private void Button_Click_1(object sender, RoutedEventArgs e)
        //{
        //    ImportData im = new ImportData();
        //    NavigationFrame.Content = im;
        //}

        private void videobtn_Click(object sender, RoutedEventArgs e)
        {
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@videoStart|#");
            if (serverStream != null)
            {
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }

            Videos V = new Videos();
            NavigationFrame.Content = V;
        }

        private void quizbtn_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            SqlCommand comm = new SqlCommand();
            try
            {
                comm.Connection = conn;
                comm.CommandText = "Delete from Result";
                comm.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to establish connection to database");
            }

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@quizStart|#");
            if (serverStream != null)
            {
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }

            QuestionAnswer QA = new QuestionAnswer();
            NavigationFrame.Content = QA;
        }

        private void reportbtn_Click(object sender, RoutedEventArgs e)
        {
            //GENERATE REPORT
            Report r = new Report();
            NavigationFrame.Content = r;
        }

        private void managequizbtn_Click(object sender, RoutedEventArgs e)
        {
            //byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@manageQuizQuestion|#");
            //if (serverStream != null)
            //{
            //    serverStream.Write(outStream, 0, outStream.Length);
            //    serverStream.Flush();
            //}

            ManageQuizModule MQM = new ManageQuizModule();
            NavigationFrame.Content = MQM;
        }

        string text = "";
        private void Page_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F5)
            {
                e.Handled = true;
            }
            else if (e.Key != Key.Back)
            {
                text = e.Key.ToString();
            }
            else if (e.Key == Key.Back)
            {
                if (text != "")
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }
    }
}